package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.BaseAccountLogs;
import com.tebiecloud.base.server.service.BaseAccountLogsService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 登录日志 前端控制器
 * @author tebie
 * @date 2019-08-02
 */
@RestController
@RequestMapping("baseaccountlogs")
public class BaseAccountLogsController {

    @Autowired
    private BaseAccountLogsService baseAccountLogsService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<BaseAccountLogs>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        BaseAccountLogs query = pageParams.mapToObject(BaseAccountLogs.class);
        QueryWrapper<BaseAccountLogs> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(baseAccountLogsService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<BaseAccountLogs> get(@RequestParam("id") Long id) {
        BaseAccountLogs entity = baseAccountLogsService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param loginTime 登录时间,
     * @param loginIp 登录Ip,
     * @param loginAgent 登录设备,
     * @param loginNums 登录次数,
     * @param userId 用户Id,
     * @param account 账号标识,
     * @param accountType 账号类型,
     * @param accountId 账号ID,
     * @param domain 账号域
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "loginTime", required = false) Date loginTime,
        @RequestParam(value = "loginIp", required = false) String loginIp,
        @RequestParam(value = "loginAgent", required = false) String loginAgent,
        @RequestParam(value = "loginNums", required = false) Integer loginNums,
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "account", required = false) String account,
        @RequestParam(value = "accountType", required = false) String accountType,
        @RequestParam(value = "accountId", required = false) Long accountId,
        @RequestParam(value = "domain", required = false) String domain
    ) {
        BaseAccountLogs entity = new BaseAccountLogs();
        entity.setLoginTime(loginTime);
        entity.setLoginIp(loginIp);
        entity.setLoginAgent(loginAgent);
        entity.setLoginNums(loginNums);
        entity.setUserId(userId);
        entity.setAccount(account);
        entity.setAccountType(accountType);
        entity.setAccountId(accountId);
        entity.setDomain(domain);
        baseAccountLogsService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param loginTime 登录时间
     * @param loginIp 登录Ip
     * @param loginAgent 登录设备
     * @param loginNums 登录次数
     * @param userId 用户Id
     * @param account 账号标识
     * @param accountType 账号类型
     * @param accountId 账号ID
     * @param domain 账号域
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "loginTime", required = false) Date loginTime,
            @RequestParam(value = "loginIp", required = false) String loginIp,
            @RequestParam(value = "loginAgent", required = false) String loginAgent,
            @RequestParam(value = "loginNums", required = false) Integer loginNums,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "accountType", required = false) String accountType,
            @RequestParam(value = "accountId", required = false) Long accountId,
            @RequestParam(value = "domain", required = false) String domain
    ) {
        BaseAccountLogs entity = new BaseAccountLogs();
        entity.setId(id);
        entity.setLoginTime(loginTime);
        entity.setLoginIp(loginIp);
        entity.setLoginAgent(loginAgent);
        entity.setLoginNums(loginNums);
        entity.setUserId(userId);
        entity.setAccount(account);
        entity.setAccountType(accountType);
        entity.setAccountId(accountId);
        entity.setDomain(domain);
        baseAccountLogsService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            baseAccountLogsService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            baseAccountLogsService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
